package com.zhongjian.technologycal.mall.activity.personal.activity;


import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.bean.BaseBean;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.LoadingDialog;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.StatusBarUtil;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.UserUtils;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/11/16.
 * 投诉建议
 */

public class SuggestionActivity extends BaseActivity {
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.feedback_et_content)
    EditText feedback_et_content;

    @Override
    protected void beforeInit() {
        super.beforeInit();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, true);
//        StatusBarUtil.setColor(this, getResources().getColor(R.color.border_color), 0);
        tv_main_title.setText("投诉建议");
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_suggestion;
    }

    /**
     * 返回上一哥页面
     */
    @OnClick(R.id.iv_back_page)
    public void onClickBack() {
        finish();
    }


    /**
     * 提交反馈建议
     */
    @OnClick(R.id.commit_tv_feedback)
    public void onClickCenterFadeback() {
        initRequest();

    }

    @Override
    public void initPresenter() {

    }

    /**
     * 投诉建议提交请求
     */
    private void initRequest() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }else if(TextUtils.isEmpty(feedback_et_content.getText().toString().trim())){
            ToastUitl.showShort("投诉建议不能为空～");
            return;
        }

        HttpRetrofitClient.getInstance(getBaseContext()).postUrlRequestAdvice(UserUtils.getUserToken(), feedback_et_content.getText().toString().trim(),
                new Subscriber<ResponseBody>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        LoadingDialog.showDialogForLoading(SuggestionActivity.this);
                    }

                    @Override
                    public void onCompleted() {
                        LoadingDialog.cancelDialogForLoading();

                    }

                    @Override
                    public void onError(Throwable e) {
                       ToastUitl.showShort(e.getMessage());
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        LoadingDialog.cancelDialogForLoading();
                        try {
                            Gson gson = new Gson();
                            BaseBean baseBean = gson.fromJson(responseBody.string(),BaseBean.class);
                            if(baseBean.getError_code() == 0){
                                ToastUitl.showShort(baseBean.getError_message());
                                finish();

                            }else if(baseBean.getError_code() == 3){

                            }else{
                                ToastUitl.showShort(baseBean.getError_message());
                            }

                        }catch (Exception e){
                            e.printStackTrace();
                        }

                    }
                });
    }


}
